package com.cykj.service.Impl;

import com.cykj.dto.ResponseDTO;
import com.cykj.mapper.UserMapper;
import com.cykj.pojo.CyAdminDo;
import com.cykj.service.UserService;
import com.cykj.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * @BelongsProject: jd-project
 * @BelongsPackage: com.cykj.service.Impl
 * @Author: 小林
 * @CreateTime: 2025-02-25 14:35
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    public ResponseDTO login(String acc, String pwd) {
         CyAdminDo login = userMapper.login(acc, pwd);
        if (login != null) {
            //创建token
            //1、创建map 存入所需的数据
             HashMap<String, Object> hashMap = new HashMap<String, Object>();
             hashMap.put("userId",login.getAdminId());
             hashMap.put("userName",login.getAdminName());
             String token = JwtUtils.generateJwt(hashMap);

             redisTemplate.opsForValue().set(token,login,30, TimeUnit.DAYS);
            return ResponseDTO.success(token);
        }
        return ResponseDTO.error("登录失败");
    }
}

